package com.fxsh168.server.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.entitys.WholeOrderEntity;
import com.fxsh168.server.utils.DecimalUtil;

import java.util.List;


public class OrderDetailsActivity extends BaseActivity {
    private TextView mOrderNum, mStartDetails, mEndDetails, mTimeDetails,
            teRoute, mIncomeDetails, mStateDetails;
    Button btnPing;
    String ids;
    LinearLayout li_show_ping;
    TextView te_user, te_me;
    TextView user_pay;//用户支付
    TextView online;//平台扣款
    TextView distance;//行驶距离
    TextView date;//行驶时间
    TextView te_user_score;//对用户的评分
    TextView te_me_score;//用户对自己的评分
    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);

        user_pay = (TextView) findViewById(R.id.user_pay);
        online = (TextView) findViewById(R.id.online);
        distance = (TextView) findViewById(R.id.distance);
        date = (TextView) findViewById(R.id.date);

        mOrderNum = (TextView) findViewById(R.id.orderNum);  //订单号
        mStartDetails = (TextView) findViewById(R.id.startDetails); //起点
        mEndDetails = (TextView) findViewById(R.id.endDetails);  //终点
        mTimeDetails = (TextView) findViewById(R.id.timeDetails);  //时间
        mIncomeDetails = (TextView) findViewById(R.id.incomeDetails);  //收入
        mStateDetails = (TextView) findViewById(R.id.stateDetails);  //订单状态
        teRoute = (TextView) findViewById(R.id.teRoute);  //线路类型
        btnPing = (Button) findViewById(R.id.btnPin);  //去评价
        te_me_score = (TextView) findViewById(R.id.te_me_score);  //对我的评分
        te_user_score = (TextView) findViewById(R.id.te_user_score);  //对用户的评分
        li_show_ping = (LinearLayout) findViewById(R.id.li_show_ping);
        te_user = (TextView) findViewById(R.id.te_uer);
        te_me = (TextView) findViewById(R.id.te_me);
        getIntentInfo();
    }

    private void getIntentInfo() {
        //获取从前页传递过来的数据
        final Intent intent = getIntent();
        if (intent != null) {
            String action = intent.getAction().trim();
            Bundle bundle = intent.getBundleExtra("bundle");
            int position = bundle.getInt("position");
            WholeOrderEntity wholeOrderEntity3 = (WholeOrderEntity) bundle.getSerializable("wholeOrderEntity");
            List<WholeOrderEntity.DataBean> data3 = wholeOrderEntity3.getData();
            WholeOrderEntity.DataBean dataBean3 = data3.get(position);
            String ddNumbers = dataBean3.getNmber().toString().trim();
            String downPlace = dataBean3.getDown().toString();
            String startPlace = dataBean3.getAddresses().toString();
            String overtime = dataBean3.getOvertime().toString().trim();
            String income = dataBean3.getPrice().toString().trim();
            String status = dataBean3.getStatus().toString().trim();
            String goTime = dataBean3.getGotime().toString().trim();
            String rid = dataBean3.getRid().toString();
            ids = dataBean3.getId().toString();
            String s = dataBean3.getPd().toString();
            mOrderNum.setText("订单编号：" + ddNumbers);
            online.setText(dataBean3.getLirun());
            distance.setText(dataBean3.getDistance());
            date.setText(dataBean3.getActual_time());
            user_pay.setText(income);
            if (dataBean3.getLirun() == null || dataBean3.getLirun().equals("")){
                dataBean3.setLirun("0");
            }
            String str = "  元";
            String total = DecimalUtil.subtract(dataBean3.getPrice(),dataBean3.getLirun()) + str;
            final SpannableStringBuilder sp = new  SpannableStringBuilder(total);
            sp.setSpan(new AbsoluteSizeSpan(10, true), DecimalUtil.subtract(dataBean3.getPrice(),dataBean3.getLirun()).length(), total.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //字体颜色
            mIncomeDetails.setText(sp);
            //将数据设置到控件上
            mStartDetails.setText(startPlace);
            mEndDetails.setText(downPlace);
            if (rid.equals("1")) {
                teRoute.setText("快车");
            } else if (rid.equals("4")) {
                teRoute.setText("专车");
            }
            li_show_ping.setVisibility(View.GONE);


            if (status.equals(3 + "")) {
                mStateDetails.setText("正在进行");
                mTimeDetails.setText(goTime);
            } else if (status.equals(4 + "")) {
                mTimeDetails.setText(goTime);
                if (s.equals("0")) {
                    btnPing.setVisibility(View.VISIBLE);
                    mStateDetails.setText("待评价");
                    mTimeDetails.setText(goTime);
                    btnPing.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent1 = new Intent(OrderDetailsActivity.this, AboutUserActivity.class);
                            intent1.putExtra("ids", ids);
                            startActivity(intent1);
                        }
                    });
                } else if (s.equals("1")) {
                    mStateDetails.setText("已完成");
                    btnPing.setVisibility(View.GONE);
                    li_show_ping.setVisibility(View.VISIBLE);
                    te_user.setText("我对用户的评价：" + data3.get(position).getComment_u());
                    te_me.setText("用户对我的评价：" + data3.get(position).getComment_content());
                    mStateDetails.setText("已完成");
                    mTimeDetails.setText(overtime);
                    String comment_score = data3.get(position).getComment_score();
                    String score_u = data3.get(position).getScore_u();
                    setIcon(score_u, comment_score);
                }
            } else if (status.equals(5 + "")) {
                mStateDetails.setText("待支付");
                mTimeDetails.setText(goTime);
            } else if (status.equals(6 + "")) {
                mStateDetails.setText("行程中");
                mTimeDetails.setText(goTime);
            } else if (status.equals(7 + "")) {
                String comment_score = data3.get(position).getComment_score();
                String score_u = data3.get(position).getScore_u();
                li_show_ping.setVisibility(View.VISIBLE);
                te_user.setText("我对用户的评价：" + data3.get(position).getComment_u());
                te_me.setText("用户对我的评价：" + data3.get(position).getComment_content());
                mStateDetails.setText("已完成");
                mTimeDetails.setText(overtime);

            } else if (status.equals("9")) {
                mStateDetails.setText("已取消");
                mTimeDetails.setText(goTime);
            }

        }
    }


    public void setIcon(String user, String driver) {
        te_user_score.setText("我对用户的评分：" + ((user == null || user.equals("")) ? 0 : user) + "颗星");
       te_me_score.setText("用户对我的评分：" + ((driver == null || driver.equals(""))  ? 0 : driver) + "颗星");
    }
}
